home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / addons / script.module.pil / lib / PIL / MpegImagePlugin.py < prev    next >
Encoding:
Python Source  |  2009-04-06  |  1.8 KB  |  83 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # MPEG file handling
  6. #
  7. # History:
  8. #       95-09-09 fl     Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1995.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16. __version__ = "0.1"
  17.  
  18. import Image, ImageFile
  19.  
  20. #
  21. # Bitstream parser
  22.  
  23. class BitStream:
  24.  
  25.     def __init__(self, fp):
  26.         self.fp = fp
  27.         self.bits = 0
  28.         self.bitbuffer = 0
  29.  
  30.     def next(self):
  31.         return ord(self.fp.read(1))
  32.  
  33.     def peek(self, bits):
  34.         while self.bits < bits:
  35.             c = self.next()
  36.             if c < 0:
  37.                 self.bits = 0
  38.                 continue
  39.             self.bitbuffer = (self.bitbuffer << 8) + c
  40.             self.bits = self.bits + 8
  41.         return self.bitbuffer >> (self.bits - bits) & (1L << bits) - 1
  42.  
  43.     def skip(self, bits):
  44.         while self.bits < bits:
  45.             self.bitbuffer = (self.bitbuffer << 8) + ord(self.fp.read(1))
  46.             self.bits = self.bits + 8
  47.         self.bits = self.bits - bits
  48.  
  49.     def read(self, bits):
  50.         v = self.peek(bits)
  51.         self.bits = self.bits - bits
  52.         return v
  53.  
  54. ##
  55. # Image plugin for MPEG streams.  This plugin can identify a stream,
  56. # but it cannot read it.
  57.  
  58. class MpegImageFile(ImageFile.ImageFile):
  59.  
  60.     format = "MPEG"
  61.     format_description = "MPEG"
  62.  
  63.     def _open(self):
  64.  
  65.         s = BitStream(self.fp)
  66.  
  67.         if s.read(32) != 0x1B3:
  68.             raise SyntaxError, "not an MPEG file"
  69.  
  70.         self.mode = "RGB"
  71.         self.size = s.read(12), s.read(12)
  72.  
  73.  
  74. # --------------------------------------------------------------------
  75. # Registry stuff
  76.  
  77. Image.register_open("MPEG", MpegImageFile)
  78.  
  79. Image.register_extension("MPEG", ".mpg")
  80. Image.register_extension("MPEG", ".mpeg")
  81.  
  82. Image.register_mime("MPEG", "video/mpeg")
  83.